package com.finalproject.endice9x9;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

//Modify layout size into square shape for game board fragment
public class SquareFrameLayout extends FrameLayout {

	public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public SquareFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SquareFrameLayout(Context context) {
		super(context);
	}

	//Convert rectangular fragment container into square fragment container
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {	
		//Retrieve fragment size from specification
		int width = MeasureSpec.getSize(widthMeasureSpec);
	    int height = MeasureSpec.getSize(heightMeasureSpec);
	    //Get smallest fragment side
	    int size = width > height ? height : width;
	    //Make new specification for given square
	    widthMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
	    heightMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	    //Set new dimensions for fragment
	    setMeasuredDimension(size, size);
	}
}
